package com.culturalCenter.dataCenter.service.impl.monitor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import com.culturalCenter.dataCenter.Utils.RedisUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.monitor.ElectricityResponse;
import com.culturalCenter.dataCenter.entity.monitor.Electricity;
import com.culturalCenter.dataCenter.service.monitor.ElectricService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 方治森
 * @since 2022/2/10
 */
@Service
public class ElectricServiceImpl implements ElectricService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<Electricity> getAll() {
        /*
          模糊查询获取keys
          for循环keys逐个获取values
          逐个填充并添加到对象集合
         */
        List<Electricity> electricList = new ArrayList<>();
        Set<String> keys = redisUtil.redisTemplate.keys("electric" + "*");
        keys.forEach(key -> {
            Map<String, String> hashValue = redisUtil.getHashValue(key);
            Electricity electricity = new Electricity();
            BeanUtil.fillBeanWithMap(hashValue, electricity, false);
            electricList.add(electricity);
        });
        return electricList;
    }

    @Override
    public ElectricityResponse getStatus() {
        /*
          模糊查询获取keys
          for循环keys逐个获取values
          逐个填充并添加到对象集合
         */
        ElectricityResponse electricityResponse = new ElectricityResponse();
        List<Electricity> electricities = new ArrayList<>();
        Double current = 0D;
        Double voltage = 0D;
        Double Temperature = 0D;
        String currentStatus = "正常";
        String voltageStatus = "正常";
        String temperatureStatus = "正常";
        String electricOut = "正常";
        Set<String> keys = redisUtil.redisTemplate.keys("electric" + "*");
        for (String key : keys) {
            Map<String, String> hashValue = redisUtil.getHashValue(key);
            Electricity electricity = new Electricity();
            BeanUtil.fillBeanWithMap(hashValue, electricity, false);
            electricities.add(electricity);
            current += ((Double.parseDouble(electricity.getCurrentA()) + Double.parseDouble(electricity.getCurrentB()) + Double.parseDouble(electricity.getCurrentC())) / 3);
            voltage += ((Double.parseDouble(electricity.getVoltageA()) + Double.parseDouble(electricity.getVoltageB()) + Double.parseDouble(electricity.getVoltageC())) / 3);
            Temperature += ((Double.parseDouble(electricity.getTemperatureA()) + Double.parseDouble(electricity.getTemperatureB()) + Double.parseDouble(electricity.getTemperatureC())) / 3);
            if (electricity.getCurrentStatus().charAt(2) == '1' || electricity.getCurrentStatus().charAt(4) == '1' || electricity.getCurrentStatus().charAt(6) == '1') {
                currentStatus = "报警";
            }
            if (electricity.getTemperatureStatus().charAt(2) == '1' || electricity.getTemperatureStatus().charAt(4) == '1' || electricity.getTemperatureStatus().charAt(6) == '1') {
                temperatureStatus = "报警";
            }
            if (electricity.getVoltageStatus().charAt(1) == '1' || electricity.getVoltageStatus().charAt(2) == '1' || electricity.getVoltageStatus().charAt(4) == '1' || electricity.getVoltageStatus().charAt(5) == '1' || electricity.getVoltageStatus().charAt(7) == '1' || electricity.getTemperatureStatus().charAt(0) == '1') {
                voltageStatus = "报警";
            }
            if (electricity.getVoltageStatus().charAt(0) == '1') {
                electricOut = "报警";
            }
        }
        // 保留2位小数
        electricityResponse.setCurrent(NumberUtil.round(current / keys.size(), 2).doubleValue());
        electricityResponse.setVoltage(NumberUtil.round(voltage / keys.size(), 2).doubleValue());
        electricityResponse.setTemperature(NumberUtil.round(Temperature / keys.size(), 2).doubleValue());
        electricityResponse.setCurrentStatus(currentStatus);
        electricityResponse.setTemperatureStatus(temperatureStatus);
        electricityResponse.setVoltageStatus(voltageStatus);
        electricityResponse.setElectricOut(electricOut);
        return electricityResponse;
    }

}
